define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'canal/index' + location.search,
                    add_url: 'canal/add',
                    edit_url: 'canal/edit',
                    del_url: 'canal/del',
                    multi_url: 'canal/multi',
                    import_url: 'canal/import',
                    table: 'canal',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'type_data', title: __('Type_data'), searchList: {"0":__('Type_data 0'),"1":__('Type_data 1')}, formatter: Table.api.formatter.status},
                        {field: 'chargeman.name', title: __('Pid'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},
                        //{field: 'openid', title: __('Openid'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'connectionNum', title: __('ConnectionNum'), operate: false,formatter: Controller.api.formatter.connectionNum},
                        /*{field: 'password', title: __('Password'), operate: 'LIKE'},
                        {field: 'salt', title: __('Salt'), operate: 'LIKE'},*/
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },formatter: {
                connectionNum: function (value, row, index) {
                    //这里手动构造URL
                    url = "connection?canal_id=" + row.id;
                    this.url = url;
                    return '<a href="' + url + '" class=" addtabsit" title="' + __("Search %s", row.name+"的客户") + '">' + __('%s', value) + '</a>';
                    //return Table.api.formatter.addtabs.call(this, value, row, index);
                }
            }
        }
    };
    return Controller;
});
